package com.qdu;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class HBasePut {
    public static void main(String[] args) {

        // 实现HBASE连接，表连接，数据插入等逻辑
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "niit-master");
        conf.setInt("hbase.rpc.timeout", 20000);
        conf.setInt("hbase.client.operation.timeout", 20000);
        conf.setInt("hbase.client.scanner.timeout.period", 20000);


        try (
                Connection conn = ConnectionFactory.createConnection(conf);
                Table tlog = conn.getTable(TableName.valueOf("t_log"))
        ) {

            // 创建Put对象，指定rowkey
            Put put = new Put(Bytes.toBytes("X1"));

            // 向Put对象中添加列族、列名和对应的值
            put.addColumn(
                    Bytes.toBytes("f"),
                    Bytes.toBytes("myname"),
                    Bytes.toBytes("李四")
            );

            // 执行数据插入操作
            tlog.put(put);
            System.out.println("Data inserted successfully.");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
